package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.ilearn.bean.Seat;
import com.jnu.ilearn.bean.SeatItem;
import com.jnu.ilearn.bean.User;
import com.jnu.ilearn.utils.DataLoader;

import java.util.ArrayList;
import java.util.List;

public class Occupied_seatActivity extends AppCompatActivity {
    List<SeatItem> seats=new ArrayList<>();
    List<SeatItem> occupied_seats=new ArrayList<>();
    int occupiedNumber=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_occupied_seat);

        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        DataLoader dataLoader=new DataLoader();
        Handler handler=new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                seats=dataLoader.GetSeat();
                for (SeatItem seat : seats) {
                    if(seat.isState()){
                        occupied_seats.add(seat);
                        occupiedNumber++;
                    }
                }
                if(occupiedNumber==0){
                    RemoveTitle();
                }
                TextView textView=findViewById(R.id.seat_Number);
                textView.setText(occupiedNumber+"");
                RecyclerView MainRecyclerView=findViewById(R.id.seat_message_recycleView);
                LinearLayoutManager layoutManager = new LinearLayoutManager(Occupied_seatActivity.this);
                MainRecyclerView.setLayoutManager(layoutManager);
                MainRecyclerView.setAdapter(new MyRecyclerViewAdapter(occupied_seats));
            }
        };
        dataLoader.LoadSeat(handler);
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter {
        private List<SeatItem> seatItems;
        public MyRecyclerViewAdapter(List<SeatItem> seatItems) {
            this.seatItems=seatItems;
            int a=0;

        }


        public  class ViewHolder extends RecyclerView.ViewHolder
        {

            private TextView seatIDView;
            private TextView userNameView;
            private TextView learnMessageView;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);

                this.setSeatIDView(itemView.findViewById(R.id.seat_ID));
                this.setUserNameView(itemView.findViewById(R.id.occupy_user));
                this.setLearnMessageView(itemView.findViewById(R.id.learn_message));

            }

            public TextView getSeatIDView() {
                return seatIDView;
            }

            public void setSeatIDView(TextView seatIDView) {
                this.seatIDView = seatIDView;
            }

            public TextView getUserNameView() {
                return userNameView;
            }

            public void setUserNameView(TextView userNameView) {
                this.userNameView = userNameView;
            }

            public TextView getLearnMessageView() {
                return learnMessageView;
            }

            public void setLearnMessageView(TextView learnMessageView) {
                this.learnMessageView = learnMessageView;
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.occupied_message, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder viewHolder=(ViewHolder) holder;

            viewHolder.getSeatIDView().setText(seatItems.get(position).getSeatID()+"");
            viewHolder.getUserNameView().setText(seatItems.get(position).getUser());
            viewHolder.getLearnMessageView().setText(seatItems.get(position).getContent());
        }

        @Override
        public int getItemCount() {
            return seatItems.size();
        }
    }
    void RemoveTitle(){
        TextView titleID=findViewById(R.id.title_ID);
        TextView user=findViewById(R.id.title_user);
        TextView content=findViewById(R.id.title_content);

        titleID.setText("");
        user.setText("");
        content.setText("");
    }
}